import os
import shutil
import threading
import time
import requests
import pyecharts
import pyecharts.options as opts
from bs4 import BeautifulSoup

url = 'https://www.apple.com.cn/retail/storelist/'
pic_url = 'images/hero_large_2x.jpg'
desktop = '/Users/lmm/Desktop/中国苹果零售店详情/'


def get_cn_stores_dict():
    # 访问网址
    r = requests.Session()
    res = r.get(url)

    # 解析网页
    html = BeautifulSoup(res.text, features="lxml")
    cn_html = html.find('div', id='cnstores')
    cn_stores_data = cn_html.find_all(name='div', attrs={"class": "toggle-section"})

    # 提取数据
    cn_stores_dict = {}  # 以城市为索引的字典，key为城市，value为城市所有零售店信息构成的list
    # 遍历城市
    for i in cn_stores_data:
        city_detail = []  # 该城市所有零售店信息，信息为字典形式
        city = i.select('h3')[0].get_text()
        stores = i.select('ul')
        # 遍历每个城市的门店
        for j in stores:
            store_detail = {}  # 每个零售店的信息
            detail = j.select('li')
            # 每个门店有3或4个li标签
            store_detail['名称'] = detail[0].get_text()
            if len(detail) == 4:
                store_detail['地址'] = detail[1].get_text() + detail[2].get_text()
            else:
                store_detail['地址'] = detail[1].get_text()
            store_detail['电话'] = detail[-1].get_text()
            store_detail['网址'] = detail[0].find_all('a')[0]['href']
            city_detail.append(store_detail)
        cn_stores_dict[city] = city_detail
    return cn_stores_dict


def print_cn_stores_dict(cn_stores_dict):
    print("中国苹果官方零售店详情：")
    for city in cn_stores_dict:
        print(city, end='')
        print(" (共计%d家零售店)" % len(cn_stores_dict[city]))
        for i in cn_stores_dict[city]:
            for k, v in i.items():
                print('\t', k, v)
            print()


def save_cn_stores_pic_threading(cn_stores_dict):
    try:
        # 删除原来的文件夹
        shutil.rmtree(desktop)
    except Exception as e:
        print(e)

    print('开始多线程下载零售店图片')
    start = time.time()
    os.mkdir(desktop)
    thread_list = []
    for city, detail in cn_stores_dict.items():
        os.mkdir(desktop + city + '/')
        for i in detail:
            # 多线程并发
            thread_list.append(threading.Thread(target=download_and_save_pic, args=(i, city,)))
    for i in thread_list:
        i.start()
    for i in thread_list:
        i.join()
    end = time.time()
    print('多线程图片下载完成，用时%.2f秒' % (end - start))
    return end - start


def download_and_save_pic(i, city):
    img = requests.get(i['网址'] + pic_url, timeout=10).content
    with open(desktop + city + '/' + i['名称'] + '.pic', 'wb') as f:
        f.write(img)


def save_cn_stores_pic(cn_stores_dict):
    try:
        shutil.rmtree(desktop)
    except Exception as e:
        print(e)

    print('开始单线程下载零售店图片')
    start = time.time()
    os.mkdir(desktop)
    for city, detail in cn_stores_dict.items():
        os.mkdir(desktop + city + '/')
        for i in detail:
            img = requests.get(i['网址'] + pic_url, timeout=10).content
            with open(desktop + city + '/' + i['名称'] + '.pic', 'wb') as f:
                f.write(img)
    end = time.time()
    print('单线程图片下载完成，用时%.2f秒' % (end - start))
    return end - start


def create_map(cn_stores_dict):
    data = [(i, len(cn_stores_dict[i])) for i in cn_stores_dict.keys()]
    map = pyecharts.charts.Map(init_opts=opts.InitOpts(width='1000px', height='700px')) \
        .add(series_name="零售店数量", data_pair=data, maptype='china', is_roam=False, zoom=1.2) \
        .set_global_opts(title_opts=opts.TitleOpts(title="中国苹果零售店分布图"),
                         visualmap_opts=opts.VisualMapOpts(max_=10, is_piecewise=True))
    map.render("res.html")
    print("中国苹果零售店分布图生成完成，请用chrome打开res.html查看。")


if __name__ == '__main__':
    cn_stores_dict = get_cn_stores_dict()
    print_cn_stores_dict(cn_stores_dict)
    duo = save_cn_stores_pic_threading(cn_stores_dict)
    dan = save_cn_stores_pic(cn_stores_dict)
    print("多线程的效率是单线程的%.3f倍" % (dan / duo))
    create_map(cn_stores_dict)
